/*
 * @Description: 新增元数据依赖配置弹窗组件
 * @Author: Rfan
 * @Date: 2022-05-30 14:43:58
 * @LastEditTime: 2022-06-16 18:42:17
 */

import { useEffect, useState } from 'react';
import { Button, Form, Modal } from 'antd';
import type { FC } from 'react';
import RelyForm from './common/RelyForm';
import { useMetadataRelyContext } from '../../contexts/MetadataRelyContext';
import { selectItemByKey } from '@/utils/list';
import { jsonClone } from '@/utils';
import type { ICommonDialog } from '@/interfaces/global';

interface EditDialogProps extends ICommonDialog {
  data: any;
}

const EditDialog: FC<EditDialogProps> = (props) => {
  const { visible, data, onClose } = props;
  const { selectAT, selectLevel, levelMatedata, editRelyFunc, generateRule } =
    useMetadataRelyContext();
  const [form] = Form.useForm();

  const [dependRuleData, setDependRuleData] = useState([]);

  useEffect(() => {
    if (visible && levelMatedata[selectLevel.level]) {
      const _data = jsonClone(data);
      const targetMetadata: any = selectItemByKey(
        _data.targetData.field_name,
        levelMatedata[data.targetData.level],
        'field_name'
      );
      _data.isLinkage = !_data.isLinkage;
      _data.rule = generateRule(_data.dependRuleData);
      _data.targetDataId = targetMetadata?.id;
      console.log('edit ', _data);
      form.setFieldsValue(_data);
    }
  }, [visible, levelMatedata]);

  // 提交表单
  const submitFunc = () => {
    form.submit();
  };

  // 新增档案类型
  const onFinish = (val: any) => {
    const values = jsonClone(val);
    const targetMetadata: any = selectItemByKey(
      values.targetDataId,
      levelMatedata[selectLevel.level]
    );
    values.archiveTypeId = selectAT.id;
    values.atCode = selectAT.atCode;
    values.atType = selectAT.atType;
    values.level = selectLevel.level;
    values.isLinkage = values.isLinkage ? 0 : 1;
    values.dependRuleData = dependRuleData;
    values.targetData = {
      field_desc: targetMetadata.field_desc,
      field_name: targetMetadata.field_name,
      level: selectLevel.level
    };
    delete values.targetDataId;
    delete values.condField;
    delete values.condMark;
    delete values.constStr;
    delete values.rule;
    console.log('edit finish ', values);
    editRelyFunc({ ...data, ...values }).then(() => {
      onCancel();
    });
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log('Failed:', errorInfo);
  };
  const onCancel = () => {
    onClose();
  };

  return (
    <Modal
      destroyOnClose
      title="编辑"
      visible={visible}
      width={600}
      onCancel={onCancel}
      footer={[
        <Button key="back" onClick={onCancel}>
          取消
        </Button>,
        <Button key="submit" type="primary" onClick={submitFunc}>
          保存
        </Button>
      ]}
    >
      <Form
        form={form}
        labelCol={{ span: 4 }}
        wrapperCol={{ span: 18 }}
        preserve={false}
        onFinish={onFinish}
        onFinishFailed={onFinishFailed}
      >
        <RelyForm changeRuleList={setDependRuleData} />
      </Form>
    </Modal>
  );
};

export default EditDialog;
